/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * dlgAddTest.java
 *
 * Created on Aug 18, 2012, 4:26:35 PM
 */

package Model.dialog;

import DataBase.DBManagerment;
import Processing.dbclass.Test;
import java.sql.Connection;
import javax.swing.JOptionPane;

/**
 *
 * @author Admin
 */
public class dlgAddTest extends javax.swing.JDialog {

    /** Creates new form dlgAddTest */
    public dlgAddTest(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

     /**
     * Đối tượng của class DBManagerment
     */
    private DBManagerment db = new DBManagerment();

    /**
     * Một Connection
     */
    private Connection con = db.ConnectServer();

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnTestAction = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        txtTestAddCost = new javax.swing.JTextField();
        jLabel38 = new javax.swing.JLabel();
        txtTestAddID = new javax.swing.JTextField();
        txtTestAddName = new javax.swing.JTextField();
        jLabel37 = new javax.swing.JLabel();
        jLabel34 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Add Test");
        setResizable(false);

        btnTestAction.setText("Add");
        btnTestAction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTestActionActionPerformed(evt);
            }
        });

        jButton6.setText("Cancel");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed1(evt);
            }
        });

        txtTestAddCost.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTestAddCostActionPerformed(evt);
            }
        });

        jLabel38.setText("Cost:");

        txtTestAddID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTestAddIDActionPerformed(evt);
            }
        });

        txtTestAddName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTestAddNameActionPerformed(evt);
            }
        });

        jLabel37.setText("Test's Name:");

        jLabel34.setText("Test's ID:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel34)
                            .addComponent(jLabel38)
                            .addComponent(jLabel37))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtTestAddName, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtTestAddCost, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtTestAddID, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(55, 55, 55)
                        .addComponent(btnTestAction, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton6)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel34)
                    .addComponent(txtTestAddID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTestAddName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel37))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTestAddCost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel38))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnTestAction)
                    .addComponent(jButton6))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_jButton6ActionPerformed

    private void btnTestActionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTestActionActionPerformed

        if (txtTestAddCost.getText().length() != 0) {
            try {
                double cost = Double.parseDouble(txtTestAddCost.getText());
                
                if (cost == 0) {
                    return;

                }
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(this, "Cost input wrong!");
                return;
            }
        }

        if (txtTestAddID.getText().length() != 3) {
            //Kiển tra nếu nhập vào ko đúng 6 ký tự thì báo lỗi

            JOptionPane.showMessageDialog(this, "Test's ID must have exactly three character!");
        }else if(db.searchTest(con, 1, txtTestAddID.getText()).getRowCount() != 0){
            // Kiểm tra xem có Patient này ko, dùng method search Patient theo PatientID
            // trả về PikaTableModel, rồi xem PikatableModel này có số row == 0 hay ko?
            // Nếu số Row == 0 tức là ko có Patient với PatientID này

            JOptionPane.showMessageDialog(this, "This Test's ID existed!");
        }else if (txtTestAddName.getText().length() == 0) {
            //Kiển tra nếu nhập vào ko đúng 3 ký tự thì báo lỗi

            JOptionPane.showMessageDialog(this, "Test's name must have!");
        } else if (txtTestAddCost.getText().length() == 0) {
            //Kiển tra nếu nhập vào ko đúng 3 ký tự thì báo lỗi

            JOptionPane.showMessageDialog(this, "Cost must have!");           
        }else if (Double.parseDouble(txtTestAddCost.getText()) < 0) {
            //Kiển tra nếu nhập vào ko đúng 3 ký tự thì báo lỗi

            JOptionPane.showMessageDialog(this, "Cost must great than 0!");
        }else{
            // Trong add bill này CollectorID có thể để trống nên ko cần kiểm tra

            // Nếu toàn bộ quá trình kiểm tra ko sai thì bắt đầu add vào

            // Tạo mới một Object Bill để chứa thông tin
            tes = new Test();

            tes.setTestID(txtTestAddID.getText());

            tes.setTestName(txtTestAddName.getText());

            tes.setCost(Double.parseDouble(txtTestAddCost.getText()));

            this.setVisible(false);

            // DateMade và DatePaid ko phải làm vì tớ đã sửa lại method add trong
            // DBManager, sẽ lấy ngày hiện tại
            // Report có DateMade tương tự cũng được tớ sủa method rồi
        }
    }//GEN-LAST:event_btnTestActionActionPerformed

    private void txtTestAddIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTestAddIDActionPerformed
        // TODO add your handling code here:
        btnTestAction.doClick();
    }//GEN-LAST:event_txtTestAddIDActionPerformed

    private void txtTestAddNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTestAddNameActionPerformed
        // TODO add your handling code here:
        btnTestAction.doClick();
    }//GEN-LAST:event_txtTestAddNameActionPerformed

    private void txtTestAddCostActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTestAddCostActionPerformed
        // TODO add your handling code here:
        btnTestAction.doClick();
    }//GEN-LAST:event_txtTestAddCostActionPerformed

    private void jButton6ActionPerformed1(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed1
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_jButton6ActionPerformed1

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                dlgAddTest dialog = new dlgAddTest(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    /**
     * Biến chứa dữ liệu của Test
     */
    private Test tes;

    /**
     * Method trả về bill để chương trình xử lý
     * @return Test
     */
    public Test getTest() {
        return tes;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnTestAction;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel34;
    private javax.swing.JLabel jLabel37;
    private javax.swing.JLabel jLabel38;
    private javax.swing.JTextField txtTestAddCost;
    private javax.swing.JTextField txtTestAddID;
    private javax.swing.JTextField txtTestAddName;
    // End of variables declaration//GEN-END:variables

}
